# 电能量当前
# 正向有功  总，费率1-4
# 组合有功  总，费率1-4
# 反向有功  总，费率1-4
# 正向有功  总，费率1-4
# 最在需量当前
# 正向有功  总，费率1-4
# 组合有功  总，费率1-4
# 反向有功  总，费率1-4
# 正向有功  总，费率1-4
# 变量
# 功率因数  总，ABC，数据块
# 电压 ABC
# 电流 ABC
import time

devs =[
#"001564772295",
#"001564772293",
"001564772292",
#"001564773410",
#"001541869457"
]
addrs =[

    {"id":0 , "c":"P" , "l":"电量组合有功-总" , "addr":[0,0,0,0]},
    {"id":1 , "c":"P" , "l":"电量组合有功-尖" , "addr":[0,0,1,0]},
    {"id":2 , "c":"P" , "l":"电量组合有功-峰" , "addr":[0,0,2,0]},
    {"id":3 , "c":"P" , "l":"电量组合有功-平" , "addr":[0,0,3,0]},
    {"id":4 , "c":"P" , "l":"电量组合有功-谷" , "addr":[0,0,4,0]},

    {"id": 20, "c":"P" , "l": "电量正向有功-总", "addr": [0, 1, 0, 0]},
    {"id": 21, "c":"P" , "l": "电量正向有功-尖", "addr": [0, 1, 1, 0]},
    {"id": 22, "c":"P" , "l": "电量正向有功-峰", "addr": [0, 1, 2, 0]},
    {"id": 23, "c":"P" , "l": "电量正向有功-平", "addr": [0, 1, 3, 0]},
    {"id": 24, "c":"P" , "l": "电量正向有功-谷", "addr": [0, 1, 4, 0]},

    {"id": 30, "c":"P" , "l": "电量反向有功-总", "addr": [0, 2, 0, 0]},
    {"id": 31, "c":"P" , "l": "电量反向有功-尖", "addr": [0, 2, 1, 0]},
    {"id": 32, "c":"P" , "l": "电量反向有功-峰", "addr": [0, 2, 2, 0]},
    {"id": 33, "c":"P" , "l": "电量反向有功-平", "addr": [0, 2, 3, 0]},
    {"id": 34, "c":"P" , "l": "电量反向有功-谷", "addr": [0, 2, 4, 0]},

    {"id": 40, "c":"P" , "l": "电量组合无功1-总", "addr": [0, 3, 0, 0]},
    {"id": 41, "c":"P" , "l": "电量组合无功1-尖", "addr": [0, 3, 1, 0]},
    {"id": 42, "c":"P" , "l": "电量组合无功1-峰", "addr": [0, 3, 2, 0]},
    {"id": 43, "c":"P" , "l": "电量组合无功1-平", "addr": [0, 3, 3, 0]},
    {"id": 44, "c":"P" , "l": "电量组合无功1-谷", "addr": [0, 3, 4, 0]},

    {"id": 50, "c":"P" , "l": "电量组合无功2-总", "addr": [0, 4, 0, 0]},
    {"id": 51, "c":"P" , "l": "电量组合无功2-尖", "addr": [0, 4, 1, 0]},
    {"id": 52, "c":"P" , "l": "电量组合无功2-峰", "addr": [0, 4, 2, 0]},
    {"id": 53, "c":"P" , "l": "电量组合无功2-平", "addr": [0, 4, 3, 0]},
    {"id": 54, "c":"P" , "l": "电量组合无功2-谷", "addr": [0, 4, 4, 0]},

    {"id": 60, "c": "Q", "l": "电压大需量正向有功-总", "addr": [1, 1, 0, 0]},
    {"id": 61, "c": "Q", "l": "电压大需量正向有功-尖", "addr": [1, 1, 1, 0]},
    {"id": 62, "c": "Q", "l": "电压大需量正向有功-峰", "addr": [1, 1, 2, 0]},
    {"id": 63, "c": "Q", "l": "电压大需量正向有功-平", "addr": [1, 1, 3, 0]},
    {"id": 64, "c": "Q", "l": "电压大需量正向有功-谷", "addr": [1, 1, 4, 0]},

    {"id": 70, "c": "Q", "l": "电压大需量反向有功-总", "addr": [1, 2, 0, 0]},
    {"id": 71, "c": "Q", "l": "电压大需量反向有功-尖", "addr": [1, 2, 1, 0]},
    {"id": 72, "c": "Q", "l": "电压大需量反向有功-峰", "addr": [1, 2, 2, 0]},
    {"id": 73, "c": "Q", "l": "电压大需量反向有功-平", "addr": [1, 2, 3, 0]},
    {"id": 74, "c": "Q", "l": "电压大需量反向有功-谷", "addr": [1, 2, 4, 0]},

    {"id": 80, "c": "Q", "l": "电压大需量组合无功1-总", "addr": [1, 3, 0, 0]},
    {"id": 81, "c": "Q", "l": "电压大需量组合无功1-尖", "addr": [1, 3, 1, 0]},
    {"id": 82, "c": "Q", "l": "电压大需量组合无功1-峰", "addr": [1, 3, 2, 0]},
    {"id": 83, "c": "Q", "l": "电压大需量组合无功1-平", "addr": [1, 3, 3, 0]},
    {"id": 84, "c": "Q", "l": "电压大需量组合无功1-谷", "addr": [1, 3, 4, 0]},

    {"id": 90, "c": "Q", "l": "电压大需量组合无功2-总", "addr": [1, 4, 0, 0]},
    {"id": 91, "c": "Q", "l": "电压大需量组合无功2-尖", "addr": [1, 4, 1, 0]},
    {"id": 92, "c": "Q", "l": "电压大需量组合无功2-峰", "addr": [1, 4, 2, 0]},
    {"id": 93, "c": "Q", "l": "电压大需量组合无功2-平", "addr": [1, 4, 3, 0]},
    {"id": 94, "c": "Q", "l": "电压大需量组合无功2-谷", "addr": [1, 4, 4, 0]},

    {"id": 100, "c": "V", "l": "A相电压", "addr": [2, 1, 1, 0]},
    {"id": 101, "c": "V", "l": "C相电压", "addr": [2, 1, 2, 0]},
    {"id": 102, "c": "V", "l": "C相电压", "addr": [2, 1, 3, 0]},

    {"id": 110, "c": "I", "l": "A相电流", "addr": [2, 2, 1, 0]},
    {"id": 111, "c": "I", "l": "C相电流", "addr": [2, 2, 2, 0]},
    {"id": 112, "c": "I", "l": "C相电流", "addr": [2, 2, 3, 0]},



]

import  serial
class TCo645:
    def __init__(self):
        self.config={
            "comPort":"COM13",
            "baud":1200
        }
        self.devs={}
        self.dps ={}

        self.ser  = None

    def init(self):
        try:
            for devCode in devs:
                dev = {
                    "devCode":devCode ,
                    "dps":{}
                }
                self.devs[devCode]= dev
                for d in addrs:
                    dkey = "_".join(['dp',devCode,str(d["id"])])
                    d['ov']=0
                    d['nv']=0
                    d['tm']=0
                    d['devCode'] = devCode
                    d['key'] = dkey
                    dev["dps"][dkey] = d
                    self.dps[dkey] = d
            dv = self.devs
        except Exception as er:
            print(er)

    def start(self):
        try:
            self.init()
            ser = serial.Serial(self.config['comPort'], self.config['baud'] , parity=serial.PARITY_EVEN)
            if ser.is_open:
                print('CIE645 collertor is start')
                while ( 1>0):
                    try:
                        for dev in self.devs.values() :
                            for dp in dev["dps"].values():
                                cmdBytes = self.getDpBytes(dp)
                                recs=[]
                                a = ser.write(cmdBytes)
                                ser.flush()
                                recData = 0
                                while recData!=0x16:
                                    respBytes = ser.read(1)
                                    if len(respBytes)>0:
                                        recData = respBytes[0]
                                        recs.append(recData)
                                print(recs)
                    except Exception as eer:
                        print(eer)

            else:
                print('CIE645 collertor is closed')
        except Exception as er:
            print(er)
    def getDpBytes(self , dp):
        res = [0x68]
        try:
            devAddr = bytes(reversed( bytes.fromhex(dp['devCode'])))
            for i in devAddr:
                res.append(i)
            res.append(0x68)
            res.append(0x11)
            res.append(0x04)
            addrs = dp['addr']
            for di in range(len(addrs)):
                pos = 4-di-1
                res.append(0x33+addrs[pos])
            cs = 0
            for i in range(len(res)):
                cs = cs + res[i]
                cs = cs % 256
            res.append(cs)
            res.append(0x16)

            txt = [hex(x)+" " for x in res]
            print(txt)
        except Exception as er:
            print(er)
        return  bytearray(res)


co645 = TCo645()
co645.start()